package com.appium.manager.element.config.ymlHandler;

import com.appium.manager.element.ElementHelper;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *     元素映射管理
 * </p>
 * @author 大忽悠
 * @create 2022/7/8 9:11
 */
public class ElementMappingHandler {
    /**
     * key:是查找方式,如:xpath,css,id,name等
     * value:是对应的查找方法,例如: findByXpath等
     */
    private final Map<String, Method> eleMappings=new HashMap<>();

    public ElementMappingHandler() {
         initMappings();
    }

    /**
     * 通过指定的查找方式字符串,定位到具体查找方法
     */
    public Method getLocateMethod(String way){
        Method method = eleMappings.get(way);
        if(method==null){
            throw new RuntimeException("不存在定位元素方式为: "+way);
        }
        return method;
    }

    /**
     * 初始化映射关系
     */
    private void initMappings()  {
        Class<ElementHelper> helperClass = ElementHelper.class;
        try {
            eleMappings.put(LocateWays.BY_ID,helperClass.getMethod("findById",String.class));

            eleMappings.put(LocateWays.BY_NAME_ONE,helperClass.getMethod("findByName",String.class));
            eleMappings.put(LocateWays.BY_NAME_LIST,helperClass.getMethod("findElementsByName",String.class));

            eleMappings.put(LocateWays.BY_CLASS_NAME_ONE,helperClass.getMethod("findByClassName",String.class));
            eleMappings.put(LocateWays.BY_CLASS_NAME_LIST,helperClass.getMethod("findElementsByClassName",String.class));

            eleMappings.put(LocateWays.BY_TAG_NAME_ONE,helperClass.getMethod("findByTagName",String.class));
            eleMappings.put(LocateWays.BY_TAG_NAME_LIST,helperClass.getMethod("findElementsByTagName",String.class));

            eleMappings.put(LocateWays.BY_XPATH_ONE,helperClass.getMethod("findByXpath",String.class));
            eleMappings.put(LocateWays.BY_XPATH_LIST,helperClass.getMethod("findElementsByXpath",String.class));

            eleMappings.put(LocateWays.BY_CSS_ONE,helperClass.getMethod("findByCssSelector",String.class));
            eleMappings.put(LocateWays.BY_CSS_LIST,helperClass.getMethod("findElementsByCssSelector",String.class));

        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 配置文件中声明的元素定位方式字符串为下面列举的其中一种
     */
    static class LocateWays{
        private static final String BY_ID="id";

        private static final String BY_NAME_ONE="nameOne";
        private static final String BY_NAME_LIST="nameList";

        private static final String BY_CLASS_NAME_ONE="classNameOne";
        private static final String BY_CLASS_NAME_LIST="classNameList";

        private static final String BY_TAG_NAME_ONE="tagNameOne";
        private static final String BY_TAG_NAME_LIST="tagNameList";

        private static final String BY_XPATH_ONE="xpathOne";
        private static final String BY_XPATH_LIST="xpathList";

        private static final String BY_CSS_ONE="cssOne";
        private static final String BY_CSS_LIST="cssList";
    }
}
